//package com.scau402.system.service.impl;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import com.scau402.common.annotation.DataScope;
//import com.scau402.common.constant.WarehouseConstants;
//import com.scau402.common.core.domain.AjaxResult;
//import com.scau402.common.core.domain.entity.SysUser;
//import com.scau402.common.utils.DateUtils;
//import com.scau402.common.utils.SecurityUtils;
//import com.scau402.common.utils.StringUtils;
//import com.scau402.common.utils.code.WarehouseCodeUtils;
//import com.scau402.common.utils.code.WarehouseStorageCodeUtils;
//import com.scau402.system.domain.*;
//import com.scau402.system.service.ISysTenantService;
//import com.scau402.system.service.IWmsWarehouseStorageService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.scau402.system1.mapper.WmsWarehouseMapper;
//import com.scau402.system.service.IWmsWarehouseService;
//import org.springframework.transaction.annotation.Transactional;
//
///**
// * 仓库Service业务层处理
// *
// * @author ruoyi
// * @date 2023-06-26
// */
//@Service
//public class WmsWarehouseServiceImpl implements IWmsWarehouseService
//{
//    @Autowired
//    private WmsWarehouseMapper wmsWarehouseMapper;
//
//    @Autowired
//    private ISysTenantService tenantService;
//
//    @Autowired
//    private IWmsWarehouseStorageService warehouseStorageService;
//
//    /**
//     * 查询仓库
//     *
//     * @param id 仓库主键
//     * @return 仓库
//     */
//    @Override
//    public WmsWarehouse selectWmsWarehouseById(Long id)
//    {
//        return wmsWarehouseMapper.selectWmsWarehouseById(id);
//    }
//
//    /**
//     * 查询仓库列表
//     *
//     * @param wmsWarehouse 仓库
//     * @return 仓库
//     */
//    @Override
//    @DataScope(tenantAlias = "t", userAlias = "u")
//    public List<WmsWarehouse> selectWmsWarehouseList(WmsWarehouse wmsWarehouse)
//    {
//        wmsWarehouse.setTenantId(SecurityUtils.getLoginUser().getTenantId());
//        return wmsWarehouseMapper.selectWmsWarehouseList(wmsWarehouse);
//    }
//
//    /**
//     * 新增仓库
//     *
//     * @param wmsWarehouse 仓库
//     * @return 结果
//     */
//    @Override
//    @Transactional
//    public int insertWmsWarehouse(WmsWarehouse wmsWarehouse)
//    {
//        wmsWarehouse.setCreateTime(DateUtils.getNowDate());
//        wmsWarehouse.setTenantId(SecurityUtils.getLoginUser().getTenantId());
//        return wmsWarehouseMapper.insertWmsWarehouse(wmsWarehouse);
//    }
//
//    /**
//     * 修改仓库
//     *
//     * @param wmsWarehouse 仓库
//     * @return 结果
//     */
//    @Override
//    public int updateWmsWarehouse(WmsWarehouse wmsWarehouse)
//    {
//        wmsWarehouse.setUpdateTime(DateUtils.getNowDate());
//        return wmsWarehouseMapper.updateWmsWarehouse(wmsWarehouse);
//    }
//
//    /**
//     * 批量删除仓库
//     *
//     * @param ids 需要删除的仓库主键
//     * @return 结果
//     */
//    @Override
//    public AjaxResult deleteWmsWarehouseByIds(Long[] ids)
//    {
//        AjaxResult ajax = AjaxResult.success();
//        List<String> success = new ArrayList<>();
//        List<String> fail = new ArrayList<>();
//        for(Long id : ids) {
//            WmsWarehouse warehouse = selectWmsWarehouseById(id);
//            // 删除仓库
//            if(warehouse.getUsedCap().equals(WarehouseConstants.WAREHOUSE_USED_CAP_EMPTY)) {
//                wmsWarehouseMapper.deleteWmsWarehouseById(id);
//                // 删除仓位
//                warehouseStorageService.deleteWmsWarehouseStorageByParentWarehouseId(warehouse.getId());
//                success.add("仓库‘"+warehouse.getWarehouseName()+"’删除成功");
//            } else {
//                fail.add("仓库‘"+warehouse.getWarehouseName()+"’当前存在货品，无法删除");
//            }
//        }
//        ajax.put("success", success);
//        ajax.put("fail", fail);
//        return ajax;
//    }
//
//    /**
//     * 删除仓库信息
//     *
//     * @param id 仓库主键
//     * @return 结果
//     */
//    @Override
//    public int deleteWmsWarehouseById(Long id)
//    {
//        return wmsWarehouseMapper.deleteWmsWarehouseById(id);
//    }
//
//    @Override
//    public String getMaxCode(SysUser user) {
//        Long tenantId = user.getTenantId();
//        return wmsWarehouseMapper.getMaxCode(tenantId);
//    }
//
//    @Override
//    public String getWarehouseCode(String loginUserTenantCode) throws InterruptedException {
//        WarehouseCodeUtils warehouseCodeUtils = new WarehouseCodeUtils();
//        return warehouseCodeUtils.getNewCode(loginUserTenantCode);
//    }
//
//    @Override
//    public Long selectWmsWarehouseByCode(WmsWarehouse warehouse) {
//        return wmsWarehouseMapper.selectWmsWarehouseByCode(warehouse);
//    }
//
//    @Override
//    public int checkDefaultExist(Long tenantId) {
//        return wmsWarehouseMapper.checkDefaultExist(tenantId);
//    }
//
//    @Override
//    public String getNewCode(SysUser user) throws InterruptedException {
//        // 获取数据库中最大的编码
//        String maxCode = getMaxCode(user);
//
//        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
//        if(StringUtils.isNull(maxCode)) {
//            maxCode = "0";
//        }
//
//        // 获取当前用户所属租户编码
//        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
//
//        SysTenant tenant = tenantService.selectSysTenantByTenantId(loginUserTenantId);
//
//        String loginUserTenantCode = tenant.getTenantCode();
//
//        // 设置redis的当前编码
//        WarehouseCodeUtils warehouseCodeUtils = new WarehouseCodeUtils();
//        warehouseCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode);
//
//        // 返回仓库编码
//        return getWarehouseCode(loginUserTenantCode);
//    }
//
//    @Override
//    public boolean checkNameExist(WmsWarehouse wmsWarehouse) {
//        if(wmsWarehouseMapper.checkNameExist(wmsWarehouse) == WarehouseConstants.WAREHOUSE_NAME_EXIST_FALSE) {
//            // 相同仓库名称不存在
//            return false;
//        } else {
//            // 相同仓库名称存在
//            return true;
//        }
//    }
//
//    /**
//     * 获取仓库包括子仓位的结构
//     * @param wmsWarehouse
//     * @param wmsStorage
//     * @return
//     */
//    @Override
//    public List<WmsWarehouse> getWarehouseAndStorage(WmsWarehouse wmsWarehouse, WmsWarehouseStorage wmsStorage) {
//        List<WmsWarehouse> warehouses = selectWmsWarehouseList(wmsWarehouse);
//        List<WmsWarehouseStorage> storages = warehouseStorageService.selectWmsWarehouseStorageList(wmsStorage);
//        Map<Long, WmsWarehouse> warehouseMap = new HashMap<>();
//        Map<Long, WmsWarehouseStorage> storageMap = new HashMap<>();
//
//        // 添加到映射 Map
//        for(WmsWarehouse warehouse : warehouses) {
//            warehouseMap.put(warehouse.getId(), warehouse);
//        }
//
//        for(WmsWarehouseStorage storage : storages) {
//            storageMap.put(storage.getId(), storage);
//        }
//
//        // 构建层次结构
//        for(WmsWarehouseStorage storage : storages) {
//            Long parentStorageId = storage.getParentStorage();
//            if(!parentStorageId.equals(WarehouseConstants.EMPTY_PARENT_STORAGE)) {
//                WmsWarehouseStorage parentStorage = storageMap.get(parentStorageId);
//                if(parentStorage != null) {
//                    parentStorage.getChildren().add(storage);
//                }
//            } else {
//                Long parentWarehouseId = storage.getParentWarehouse();
//                WmsWarehouse parentWarehouse = warehouseMap.get(parentWarehouseId);
//                if(parentWarehouse != null) {
//                    parentWarehouse.getChildrenStorages().add(storage);
//                }
//            }
//        }
//
//        return warehouses;
//    }
//
//    @Override
//    public boolean checkInOK(WmsInboundOrder wmsInboundOrder) {
//        // 检查仓库是否允许入库
//        WmsWarehouse warehouse = selectWmsWarehouseById(wmsInboundOrder.getDesWarehouseId());
//        // 仓库不存在
//        if(warehouse == null) {
//            return false;
//        }
//        // 仓库类型为不准入库
//        if(warehouse.getIsInOk().equals(WarehouseConstants.WAREHOUSE_IN_NOT_OK)) {
//            return false;
//        }
//        return true;
//    }
//
//    @Override
//    public boolean checkFull(WmsInboundOrder wmsInboundOrder) {
//        WmsWarehouse warehouse = selectWmsWarehouseById(wmsInboundOrder.getDesWarehouseId());
//        // 检查仓库容量是否已满
//        if(!warehouse.getMaxCap().equals(WarehouseConstants.WAREHOUSE_MAXCAP_INFINITE) && ((warehouse.getUsedCap()+wmsInboundOrder.getTotalQuantity()) > warehouse.getMaxCap())) {
//            // 容量已满
//            return false;
//        }
//        return true;
//    }
//
//    /**
//     * 更新仓库数量
//     * @param wmsInboundOrder
//     * @return
//     */
//    @Override
//    public int updateQuantity(WmsInboundOrder wmsInboundOrder) {
//        WmsWarehouse warehouse = selectWmsWarehouseById(wmsInboundOrder.getDesWarehouseId());
//        warehouse.setUsedCap(warehouse.getUsedCap() + wmsInboundOrder.getTotalQuantity());
//        return(updateWmsWarehouse(warehouse));
//    }
//
//    @Override
//    public boolean checkOutOK(WmsOutboundOrder wmsOutboundOrder) {
//        // 检查仓库是否允许出库
//        WmsWarehouse warehouse = selectWmsWarehouseById(wmsOutboundOrder.getWarehouseId());
//        // 仓库不存在
//        if(warehouse == null) {
//            return false;
//        }
//        // 仓库类型为不准出库
//        if(warehouse.getIsOutOk().equals(WarehouseConstants.WAREHOUSE_OUT_NOT_OK)) {
//            return false;
//        }
//        return true;
//    }
//}
